package com.qk.controller;

import com.qk.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.service.RoleService;
import com.qk.service.impl.RoleServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/roles")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    //1. 角色列表查询
    @GetMapping
    public Result roleList(String name, String label,
                           @RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("角色列表查询-name={},label={},page={},pageSize={}",name,label,page,pageSize);
        PageResult pageResult =roleService.roleList(name, label, page, pageSize);
        return Result.success(pageResult);
    }

    //2. 删除角色
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        log.info("角色删除-id={}",id);
        roleService.deleteById(id);
        return Result.success();
    }

    //3. 添加角色
    @PostMapping
    public Result addRole(@RequestBody Role role){
        log.info("添加角色:{}",role);
        roleService.addRole(role);
        return Result.success();
    }

    //4. 根据ID查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("查询-id={}",id);
        Role role=roleService.getById(id);
        return Result.success(role);
    }

    //5. 修改角色
    @PutMapping
    public Result updateById(@RequestBody Role role){
        log.info("修改角色:{}",role);
        role.setUpdateTime(LocalDateTime.now());
        roleService.updateById(role);
        return Result.success();
    }

    //6. 查询所有角色
    @GetMapping("/list")
    public Result selectList(){
        log.info("查询所有角色");
        List<Role> list=roleService.selectList();
        return Result.success(list);
    }

}
